import React from 'react'
import request from '@/utils/request'
import { withUser } from '@/utils/hoc'
import { withRouter } from 'react-router-dom'
// import PropTypes from 'prop-types'
import {
	message,
	Form,
	Input,
	Button,
	Radio,
	Select,
	Cascader,
	DatePicker,
	InputNumber,
	TreeSelect,
	Switch,
	Rate,
	Upload,
} from 'antd'
import { UploadOutlined, InboxOutlined } from '@ant-design/icons'
const { Option } = Select
function Bd({ userInfo, history, data }) {
	const value = [
		'现代言情',
		'古代言情',
		'玄幻言情',
		'仙侠奇缘',
		'悬疑',
		'科幻空间',
		'游戏竞技',
		'短篇小说',
		'轻小说',
	]
	const [form] = Form.useForm()
	const isEdit = !!data
	console.log(data, isEdit)
	const onFinish = async function (xx) {
		console.log(xx)
		if (isEdit) {
			const { data } = await request.post('/goods/update', xx)
			console.log('data=', data)
			if (data.code === 200) {
				message.success('修改成功')
			}
		} else {
			const { type, zt, ...value } = xx
			const data1 = {
				type: type + zt,
				...value,
			}
			const { data } = await request.post('/goods/add', data1)
			if (data.code === 200) {
				message.success('添加成功')
			}
		}
	}
	console.log('myform', form)

	// 编辑状态
	if (isEdit) {
		form.setFieldsValue(data[0])
	}
	return (
		<Form
			labelCol={{
				span: 4,
			}}
			wrapperCol={{
				span: 18,
			}}
			layout="horizontal"
			form={form}
			onFinish={onFinish}
		>
			<Form.Item label="小说名" name="title">
				<Input />
			</Form.Item>
			<Form.Item label="作者" name="autho">
				<Input />
			</Form.Item>
			{isEdit ? (
				<>
					<Form.Item label="封面" name="img_url">
						<Input />
					</Form.Item>
					<Form.Item label="_id" name="_id" hidden>
						<Input />
					</Form.Item>
				</>
			) : (
				<>
					<Form.Item
						name="upload"
						label="封面"
						// valuePropName="fileList"
						// getValueFromEvent={normFile}
					>
						<Upload
							name="file"
							action="http://39.104.203.107:2333/goods/upload"
							enctype="multipart/form-data"
							method="post"
							onChange={(file) => {
								console.log(file)
							}}
							// name="file"
							// multiple={true}
							// listType="picture"
						>
							<Button icon={<UploadOutlined />}>
								点击上传图片
							</Button>
						</Upload>
					</Form.Item>
					<Form.Item label="分类" name="type">
						<Select style={{ width: 120 }}>
							{value.map((item) => {
								return <Option value={item}>{item}</Option>
							})}
						</Select>
					</Form.Item>
					<Form.Item label="是否完结" name="zt">
						<Select style={{ width: 120 }}>
							<Option value="已完结">已完结</Option>
							<Option value="连载中">连载中</Option>
						</Select>
					</Form.Item>
				</>
			)}
			<Form.Item label="简介" name="introduction">
				<Input.TextArea style={{ height: 200 }} />
			</Form.Item>

			<Form.Item
				wrapperCol={{
					offset: 4,
				}}
			>
				<Button
					type="primary"
					htmlType="submit"
					style={{ marginRight: 20, backgroundColor: 'green' }}
				>
					提交
				</Button>
				{isEdit ? (
					''
				) : (
					<>
						<Button
							type="primary"
							onClick={() => {
								form.resetFields()
							}}
						>
							重置
						</Button>
					</>
				)}
			</Form.Item>
		</Form>
	)
}

// props数据类型校验
Bd.propTypes = {
	// 定义data对象的属性类型
	// data:PropTypes.shape({
	//     _id: PropTypes.string,
	//     question: PropTypes.string.isRequired
	// })
}

// props默认值
// InterviewForm.defaultProps = {
//     data:{}
// }

Bd = withUser(Bd)
Bd = withRouter(Bd)

export default Bd
